package com.start.workflow.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.exceptions.StartBusException;
import com.start.core.annotations.RegisterPermission;
import com.start.workflow.core.manage.StartActUserTaskManage;
import com.start.workflow.entity.StartActTaskEntity;
import com.start.workflow.vo.StartCustomActAuthUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "全局流程任务管理")
@RestController
@RequestMapping("/start/actTask")
public class StartActTaskController {

    @Autowired
    private StartActUserTaskManage startActUserTaskManage;


    @Operation(summary = "分页查询流程正在执行的任务")
    @GetMapping("/pageActTask")
    @PreAuthorize("@pas.hasPermission('start:act:task:info')")
    @RegisterPermission(value = "start:act:task:info", name = "分页查询流程正在执行的任务")
    public Result<?> pageActTask(Page<StartActTaskEntity> page) {
        QueryWrapper<StartActTaskEntity> queryWrapper = DataQueryConstructor.createEntity(StartActTaskEntity.class);
        return Result.ok(startActUserTaskManage.pageActTask(page, queryWrapper));
    }

    @Operation(summary = "查询流程任务全部候选处理人")
    @GetMapping("/listTaskCandidateUser/{taskId}")
    @PreAuthorize("@pas.hasPermission('start:act:task:candidate_user:info')")
    @RegisterPermission(value = "start:act:task:candidate_user:info", name = "查询流程任务全部候选处理人")
    public Result<?> listTaskCandidateUser(@PathVariable("taskId") String taskId) {
        return Result.ok(startActUserTaskManage.listTaskCandidateUser(taskId));
    }

    @Operation(summary = "流程任务添加候选处理人")
    @PostMapping("/addTaskCandidateUser/{taskId}")
    @PreAuthorize("@pas.hasPermission('start:act:task:candidate_user:add')")
    @RegisterPermission(value = "start:act:task:candidate_user:add", name = "流程任务添加候选处理人")
    public Result<?> addTaskCandidateUser(@PathVariable("taskId") String taskId, @RequestBody StartCustomActAuthUser actAuthUser) {
        if (StringUtils.isAllBlank(actAuthUser.getUserId())) {
            throw new StartBusException("用户id不能为空");
        }
        if (StringUtils.isAllBlank(actAuthUser.getStartUserType())) {
            throw new StartBusException("用户体系类型不能唯空");
        }
        startActUserTaskManage.addUserTaskCandidateUser(taskId, actAuthUser);
        return Result.ok();
    }

    @Operation(summary = "流程任务删除选处理人")
    @DeleteMapping("/delTaskCandidateUser/{taskId}")
    @PreAuthorize("@pas.hasPermission('start:act:task:candidate_user:del')")
    @RegisterPermission(value = "start:act:task:candidate_user:del", name = "流程任务删除选处理人")
    public Result<?> delTaskCandidateUser(@PathVariable("taskId") String taskId, @RequestBody List<StartCustomActAuthUser> startCustomActAuthUsers) {
        startActUserTaskManage.delTaskCandidateUser(taskId, startCustomActAuthUsers);
        return Result.ok();
    }

}
